import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:get/get.dart';

class homeLogic extends GetxController {
  RxString msg = ''.obs;
  var _data = [
    Item(Icons.map, 'Icons.map'),
    Item(Icons.error, 'Icons.error'),
    Item(Icons.update, 'Icons.update'),
    Item(Icons.code, 'Icons.code'),
    Item(Icons.ac_unit, 'Icons.ac_unit'),
  ];

  onNotification(scrollNotification) {
    // return true;
    // 注册通知回调
    if (scrollNotification is ScrollStartNotification) {
      // 滚动开始
      debugPrint('Scroll Start');
    } else if (scrollNotification is ScrollUpdateNotification) {
      // 滚动位置更新
      debugPrint('Scroll Update');
    } else if (scrollNotification is ScrollEndNotification) {
      // 滚动结束
      debugPrint('Scroll End');
    }
    isTop = scrollNotification.metrics.pixels == 0;
    isBottom = scrollNotification.metrics.pixels ==
        scrollNotification.metrics.maxScrollExtent;
    if (isBottom) toTop();
    return false;
  } //note:被回收后，使用这个方法就会报错

  List<Item> get datas => _data;
  bool? _isTop, _isBottom;

  bool get isBottom => _isBottom ?? false;

  bool get isTop => _isTop ?? true;

  set isBottom(bool isBottom) {
    _isBottom = isBottom;
  }

  set isTop(bool isTop) {
    _isTop = isTop;
  }

  ScrollController? _controllerScroll;

  ScrollController get controllerScroll {
    // _controllerScroll.addListener(() {_controllerScroll.offset });
    return _controllerScroll ??= ScrollController();
  }

  void toTop() {
    controllerScroll.animateTo(
        .0 /* controllerScroll.position.maxScrollExtent//底*/,
        duration: Duration(milliseconds: 200),
        curve: Curves.ease); // 做一个滚动到顶部的动画
  }

  @override
  void onReady() {
    var map = Get.arguments;
    msg.value = map['msg'];
    super.onReady();
  }

  @override
  void onClose() {
    _controllerScroll?.dispose();
  }

  void _remove(int index) {
    datas.removeAt(index);
    update();
  }

  void _addAlarm() {
    datas.add(Item(Icons.access_alarm, Icons.access_alarm.toString()));
    update();
  }

  void _upTitle(int index) {
    datas[index].title += '$index';
    update();
  }

  onlongpress(int index) {
    return index % 2 == 1 ? () => _remove(index) : () => _addAlarm();
  }

  onTap(int index) {
    return index % 2 == 0 ? () => _upTitle(index) : null;
  }
}

class Item {
  final IconData iconData;
  String title;

  Item(this.iconData, this.title);
}
